using Microsoft.AspNetCore.Mvc;
using WMS_Goodsinfo.Infastructure.Interface;
using WMS_Goodsinfo.Domain;

namespace WMS_Goodsinfo.Api.Write.Controllers
{
    /// <summary>
    /// 预测控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class PredictionController : ControllerBase
    {
        private readonly IPredictionService _predictionService;

        public PredictionController(IPredictionService predictionService)
        {
            _predictionService = predictionService;
        }

        /// <summary>
        /// 生成汇总数据
        /// </summary>
        [HttpPost("GenerateSummaryData")]
        public async Task<IActionResult> GenerateSummaryData([FromBody] GenerateSummaryRequest request)
        {
            try
            {
                var result = await _predictionService.GenerateSummaryDataAsync(
                    request.ProductId, 
                    request.StartDate, 
                    request.EndDate);

                return Ok(new { success = result, message = result ? "汇总数据生成成功" : "汇总数据生成失败" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 生成特征数据
        /// </summary>
        [HttpPost("GenerateFeatures")]
        public async Task<IActionResult> GenerateFeatures([FromBody] GenerateFeaturesRequest request)
        {
            try
            {
                var result = await _predictionService.GenerateFeaturesAsync(
                    request.ProductId, 
                    request.Date);

                return Ok(new { success = result, message = result ? "特征数据生成成功" : "特征数据生成失败" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 训练模型
        /// </summary>
        [HttpPost("TrainModel")]
        public async Task<IActionResult> TrainModel([FromBody] TrainModelRequest request)
        {
            try
            {
                var result = await _predictionService.TrainModelAsync(request.ProductId);

                return Ok(new { success = result, message = result ? "模型训练成功" : "模型训练失败" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 执行预测
        /// </summary>
        [HttpPost("PredictOutbound")]
        public async Task<IActionResult> PredictOutbound([FromBody] PredictRequest request)
        {
            try
            {
                var prediction = await _predictionService.PredictOutboundAsync(
                    request.ProductId, 
                    request.PredictionDate);

                return Ok(new { 
                    success = true, 
                    prediction = prediction,
                    message = $"预测出库数量: {prediction}" 
                });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 获取预测历史
        /// </summary>
        [HttpGet("GetPredictionHistory")]
        public async Task<IActionResult> GetPredictionHistory(
            [FromQuery] int productId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            try
            {
                var history = await _predictionService.GetPredictionHistoryAsync(
                    productId, startDate, endDate);

                return Ok(new { success = true, data = history });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }
    }

    #region 请求模型

    public class GenerateSummaryRequest
    {
        public int ProductId { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }

    public class GenerateFeaturesRequest
    {
        public int ProductId { get; set; }
        public DateTime Date { get; set; }
    }

    public class TrainModelRequest
    {
        public int ProductId { get; set; }
    }

    public class PredictRequest
    {
        public int ProductId { get; set; }
        public DateTime PredictionDate { get; set; }
    }

    #endregion
} 